package org.jeecg.modules.communityDynamicManag.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.communityDynamicManag.entity.*;
import org.jeecg.modules.communityDynamicManag.mapper.CommunityDynamicManagMapper;
import org.jeecg.modules.communityDynamicManag.service.ICommunityDynamicManagService;
import org.jeecg.modules.communityInfluence.entity.CommunityInfluence;
import org.jeecg.modules.communityInfluence.mapper.CommunityInfluenceMapper;
import org.jeecg.modules.ruleOfInfluence.entity.RuleOfInfluence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Description: 社团动态管理
 * @Author: jeecg-boot
 * @Date: 2020-07-21
 * @Version: V1.0
 */
@Service
public class CommunityDynamicManagServiceImpl extends ServiceImpl<CommunityDynamicManagMapper, CommunityDynamicManag> implements ICommunityDynamicManagService {

    @Autowired
    CommunityDynamicManagMapper communityDynamicManagMapper;

    @Autowired
    CommunityInfluenceMapper communityInfluenceMapper;

    /**
     * 分页查询
     *
     * @param pageList
     * @param communityDynamicManag
     * @return
     */
    @Override
    public Page<CommunityDynamicManagResult> getPageList(Page<CommunityDynamicManagResult> pageList, CommunityDynamicManagResult communityDynamicManag) {
        return pageList.setRecords(communityDynamicManagMapper.getPageList(communityDynamicManag));
    }

    /**
     * 当前动态点赞
     *
     * @param form
     */
    @Override
    @Transactional
    public Boolean saveLikes(ClubDynamicsLikesEntity form) {

        Boolean bool;

        //判断当前用户是否已经点赞
        int i = communityDynamicManagMapper.selById(form);

        if (i > 0) {
            // 如火i>0则当前用户已经点赞，再执行点赞操作则取消点赞
            communityDynamicManagMapper.delById(form);
        } else {
            // 否则，执行添加操作
            // TODO 暂时使用后台登陆用户信息，后期手机登陆与后台登陆用户要区别开
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            if (sysUser != null) {
                form.setCreateBy(sysUser.getUsername());
                form.setUpdateBy(sysUser.getUsername());
            }
            form.setCreateTime(new Date());
            form.setUpdateTime(new Date());
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "");
            form.setId(uuid);
            communityDynamicManagMapper.save(form);
            // 点赞社团动态之后根据用户ID获取该用户所在社团

        }
        return null;

    }

    /**
     * 获取点赞用户列表,分页查询
     *
     * @param pageList
     * @return
     */
    @Override
    public Page<ClubDynamicsLikesEntityResult> getLikesUser(Page<ClubDynamicsLikesEntityResult> pageList) {
        return pageList.setRecords(communityDynamicManagMapper.getLikesUser());
    }

    /**
     * 动态审核
     *
     * @param form
     * @return
     */
    @Override
    @Transactional
    public Boolean dynamicReview(CommunityDynamicManag form) {
        Boolean bool = false;
        bool = communityDynamicManagMapper.dynamicReview(form);
        if (bool) {
            // 获取社团影响力规则
            RuleOfInfluence ruleOfInfluence = communityDynamicManagMapper.getRuleOfInfluence();
            // 根据社团ID查询社团影响力
            CommunityInfluence communityInfluence = communityDynamicManagMapper.getInfluenceInfo(form.getCommunityId());

            if (communityInfluence.getTotalInfluence() >= ruleOfInfluence.getLevelTwoInfluence()) {
                // 当前影响力大于等于LV2所需要影响力则更新社团影响力等级
                communityDynamicManagMapper.updateLevel("2");
            } else if (communityInfluence.getTotalInfluence() >= ruleOfInfluence.getLevelThreeInfluence()) {
                // 当前影响力大于等于LV3所需要影响力则更新社团影响力等级
                communityDynamicManagMapper.updateLevel("3");
            }

            int i = 0;

            // 获取当天时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = sdf.format(date);
            // 查询当天发布动态已经获取了多少影响力
            CommunityDynamicManagResult entity = new CommunityDynamicManagResult();
            entity.setCommunityId(form.getCommunityId());
            entity.setSpare(dateTime);
            Integer num = communityDynamicManagMapper.getMostDayDynamic(entity);
            int sum = num * ruleOfInfluence.getSingleDynamic();
            if (sum >= ruleOfInfluence.getMostDayDynamic()) {
                // 如果当天发布动态影响力总值大于等于规则值则影响力不增加
                return bool;
            } else {
                if ((sum + ruleOfInfluence.getSingleDynamic()) > ruleOfInfluence.getMostDayDynamic()) {
                    // 当天发布动态还能增加多少影响力
                    int j = ruleOfInfluence.getMostDayDynamic() - sum;
                    i = communityInfluence.getTotalInfluence() + j;
                    // 更新影响力
                    communityInfluence.setTotalInfluence(i);
                    bool = communityDynamicManagMapper.updateInfluence(communityInfluence);
                    return bool;
                } else {
                    // 发布动态审核通过后社团总影响力
                    i = ruleOfInfluence.getSingleDynamic() + communityInfluence.getTotalInfluence();
                    // 更新影响力
                    communityInfluence.setTotalInfluence(i);
                    bool = communityDynamicManagMapper.updateInfluence(communityInfluence);
                    return bool;
                }
            }
        }
        return bool;
    }
}
